<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
        span{
            position: absolute;
            left: 0;
            top:200px;
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <button>点了才会走</button>
    <div></div>
    <span>赵哲</span>
    <script>
        // 给不同元素指定了不同的定时器
       
        function han(a,b){
            // 先清除以前的定时器 只保留一个定时器 不然一直点会一直加速前进
            clearInterval(a.timer);
            a.timer = setInterval(function() {
            if(a.offsetLeft >= b){
                // 停止动画 本质是停止计时器
                clearInterval(a.timer);
            }
           a.style.left = a.offsetLeft + 3 + 'px';
        },30)
        }
        var div = document.querySelector('div');
        var span = document.querySelector('span');
        var btn = document.querySelector('button');
        han(div,200)
        btn.addEventListener('click',function(){
            han(span,300)
        })
            
        
    </script>
</body>
</html>